package com.wwwarehouse.usercenter.fragment.permissiondistribute;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.adapter.carddesk.MyFragmentPagerAdapter;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.authoritydistribute.GetFailDetailResponseBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author : mundane
 * @time : 2017/9/24 11:42
 * @description :
 * @file : AuthorityDistributeFailAuthDetailPageFragment.java
 */

public class AuthorityDistributeFailAuthDetailPageFragment extends BaseFragment {

	private CustomViewPagerInternal mCustomViewPagerInternal;
	private List<Fragment> mFragmentList;
	private MyFragmentPagerAdapter mViewPagerAdapter;
	private CirclePageIndicator mCirclePagerIndicator;
	private String mProcessId;
	public static final int PAGE_SIZE = 8;
	private int mFragmentListSize;
	private NoHttpUtils.OnResponseListener mResponseListener = new NoHttpUtils.OnResponseListener() {
		@Override
		public void onSucceed(CommonClass commonClass, int what) {
			switch (what) {
				case 0:
					if (TextUtils.equals("0", commonClass.getCode())) {
						mStateLayout.showContentView();
						String data = commonClass.getData().toString();
						LogUtils.showInfoLog("data = " + data);
						GetFailDetailResponseBean bean = JSON.parseObject(data, GetFailDetailResponseBean.class);
						int total = bean.total;
						int remainder = total % PAGE_SIZE;
						if (remainder == 0) {
							mFragmentListSize = total / PAGE_SIZE;
						} else {
							mFragmentListSize = total / PAGE_SIZE + 1;
						}
						for (int i = 0; i < mFragmentListSize; i++) {
							mFragmentList.add(AuthorityDistributeFailDetailListFragment.newInstance(i + 1 + "", mProcessId));
						}
						mViewPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), mFragmentList);
						mCustomViewPagerInternal.setAdapter(mViewPagerAdapter);
						mCirclePagerIndicator.setViewPager(mCustomViewPagerInternal);

//						GetSuccessDetailResponseBean bean = JSON.parseObject(data, GetSuccessDetailResponseBean.class);
//						int total = bean.total;
//						int remainder = total % PAGE_SIZE;
//						if (remainder == 0) {
//							mFragmentListSize = total / PAGE_SIZE;
//						} else {
//							mFragmentListSize = total / PAGE_SIZE + 1;
//						}
//
//						for (int i = 0; i < mFragmentListSize; i++) {
//							mFragmentList.add(AuthorityDistributeSuccessDetailListFragment.newInstance( i + 1 + "", mProcessId));
//						}
//						mViewPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), mFragmentList);
//						mCustomViewPagerInternal.setAdapter(mViewPagerAdapter);
//						mCirclePagerIndicator.setViewPager(mCustomViewPagerInternal);

					} else {
						ToastUtils.showToast(commonClass.getMsg());
					}
					break;
			}
		}

		@Override
		public void onFailed(String s, int i) {

		}

		@Override
		public void onFinish(int i) {

		}
	};
	private StateLayout mStateLayout;
	private static final String KEY_TYPE = "key_type";
	private int type;

	public static AuthorityDistributeFailAuthDetailPageFragment newInstance(String processId, int type) {
		AuthorityDistributeFailAuthDetailPageFragment fragment = new AuthorityDistributeFailAuthDetailPageFragment();
		Bundle bundle = new Bundle();
		bundle.putString(UserCenterConstant.KEY_PROCESS_ID, processId);
		bundle.putInt(KEY_TYPE, type);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null) {
			mProcessId = bundle.getString(UserCenterConstant.KEY_PROCESS_ID);
			type = bundle.getInt(KEY_TYPE);
		}
		mFragmentList = new ArrayList<>();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_page_detail_distribute_authority, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		// findViewById
		mCustomViewPagerInternal = $(R.id.cvpi);
		mCirclePagerIndicator = $(R.id.cpi);
		mStateLayout = $(R.id.slt);
		mStateLayout.showProgressView(true);
//		for (int i = 0; i < 3; i++) {
//			mFragmentList.add(AuthorityDistributeFailDetailListFragment.newInstance());
//		}
//		mViewPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), mFragmentList);
//		mCustomViewPagerInternal.setAdapter(mViewPagerAdapter);
//        mCirclePagerIndicator.setViewPager(mCustomViewPagerInternal);
	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof AuthorityDistributeFailAuthDetailPageFragment) {
			mActivity.setTitle("未接收成功的权限");
		}
	}

	@Override
	public void requestDatas() {
		if (TextUtils.isEmpty(mProcessId)) {
			return;
		}
		Map<String, Object> map = new HashMap<>();
		map.put("page", 1);
		map.put("processId", mProcessId);
		map.put("size", PAGE_SIZE);
		map.put("type", type);
		NoHttpUtils.httpPost(UserCenterConstant.GET_AUTH_FAIL_DETAIL, map, mResponseListener, 0);

	}
}
